package cn.cc.goods.controller.controller;

import cn.cc.common.beanCopy.utils.BeanCopierUtil;
import cn.cc.common.result.utils.RestResult;
import cn.cc.common.result.utils.RestResultEmum;
import cn.cc.common.utils.MapToBeans;
import cn.cc.goods.controller.dto.AddGoodsDTO;
import cn.cc.goods.controller.dto.DelGoodsDTO;
import cn.cc.goods.controller.dto.UpdGoodsDTO;
import cn.cc.goods.controller.vo.EsResultSourceVO;
import cn.cc.goods.controller.vo.GetGoodsSkuAndSpuVO;
import cn.cc.goods.service.GoodsService;
import cn.cc.goods.service.bo.AddGoodsSkuAndSpuBO;
import cn.cc.goods.service.bo.DelGoodsSkuAndSpuBO;
import cn.cc.goods.service.bo.UpdGoodsSkuAndSpuBO;
import cn.cc.goods.service.domain.EsResultSourceDO;
import cn.cc.goods.service.domain.GetGoodsSkuAndSpuDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.swing.plaf.basic.BasicEditorPaneUI;
import javax.validation.constraints.PastOrPresent;
import java.io.IOException;
import java.util.*;

/**
 * @program: smail-mi-cloud
 * @description:商品主体功能接口
 * @author: Mr.WuChao
 * @create: 2021-07-17 11:34
 **/
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    /**
     * @description:用户点击商品获取具体商品sku+spu
     * @auther: Mr.WuChao
     * @date: 2021/7/17 11:35
     * @return: cn.cc.common.result.utils.RestResult<cn.cc.goods.controller.vo.GetGoodsSpuVO>
     */
    @PostMapping("/checkGoods")
    public RestResult<GetGoodsSkuAndSpuVO> checkGoods(String goodsId) {
        RestResult<GetGoodsSkuAndSpuDO> skuAndSpu = goodsService.getGoodsSkuAndSpu(goodsId);
        GetGoodsSkuAndSpuVO skuAndSpuVO = BeanCopierUtil.copyObj(skuAndSpu.getData(), GetGoodsSkuAndSpuVO.class);
        if (null == skuAndSpu) {
            return new RestResult<>(RestResultEmum.GET_ERROR.code, RestResultEmum.GET_ERROR.msg);
        }
        return new RestResult<>(skuAndSpuVO);
    }

    /**
     * @description:模糊查询商品
     * @auther: Mr.WuChao
     * @date: 2021/7/20 11:54
     * @return: cn.cc.common.result.utils.RestResult<java.util.List < java.util.Map < java.lang.String, java.lang.Object>>>
     */
    @PostMapping("/likeGoods")
    public RestResult<Map<String, EsResultSourceVO>> likeGoods(String index, String spuName) throws IOException {
        RestResult<Map<String, EsResultSourceDO>> restResult = goodsService.esGetSpuName(index, spuName);
        Map<String, EsResultSourceVO> map = new HashMap<>();
        //所有key值
        List<String> list1 = new ArrayList<>();
        //所有values
        List<EsResultSourceVO> list2 = new ArrayList<>();
        //拿到所有数据
        Map<String, EsResultSourceDO> data = restResult.getData();
        Iterator<EsResultSourceDO> iterator = data.values().iterator();
        while (iterator.hasNext()) {
            EsResultSourceDO next = iterator.next();
            EsResultSourceVO vo = new EsResultSourceVO();
            EsResultSourceVO copyObj = BeanCopierUtil.copyObj(next, vo);
            list2.add(copyObj);
        }
        Iterator<String> iterator1 = data.keySet().iterator();
        while (iterator1.hasNext()) {
            String next1 = iterator1.next();
            list1.add(next1);
        }
        for (int i = 0; i < list1.size() && i < list2.size(); i++) {
            String s = list1.get(i);
            EsResultSourceVO vo = list2.get(i);
            map.put(s, vo);
        }
        return new RestResult<>(map);
    }


    @PostMapping("/addGoods")
    public RestResult<Void> addGoods(AddGoodsDTO addGoodsDTO) {
        AddGoodsSkuAndSpuBO bo = BeanCopierUtil.copyObj(addGoodsDTO, AddGoodsSkuAndSpuBO.class);
        RestResult<Void> result = goodsService.addGoods(bo);
        if (!result.getCode().equals(RestResultEmum.SUCCESS.code)) {
            return new RestResult<>(RestResultEmum.SAVE_ERROR.code, RestResultEmum.SAVE_ERROR.msg);
        }
        return new RestResult<>();
    }

    @PostMapping("/updGoods")
    public RestResult<Void> updGoods(UpdGoodsDTO updGoodsDTO) {
        UpdGoodsSkuAndSpuBO bo = BeanCopierUtil.copyObj(updGoodsDTO, UpdGoodsSkuAndSpuBO.class);
        RestResult<Void> result = goodsService.updGoods(bo);
        if (!result.getCode().equals(RestResultEmum.SUCCESS.code)) {
            return new RestResult<>(RestResultEmum.EDIT_ERROR.code, RestResultEmum.EDIT_ERROR.msg);
        }
        return new RestResult<>();
    }

    @PostMapping("/delGoods")
    public RestResult<Void> delGoods(DelGoodsDTO delGoodsDTO) {
        DelGoodsSkuAndSpuBO bo = BeanCopierUtil.copyObj(delGoodsDTO, DelGoodsSkuAndSpuBO.class);
        RestResult<Void> result = goodsService.delGoods(bo);
        if (!result.getCode().equals(RestResultEmum.SUCCESS.code)) {
            return new RestResult<>(RestResultEmum.DEL_ERROR.code, RestResultEmum.DEL_ERROR.msg);
        }
        return new RestResult<>();
    }
}
